/**
 * @createtime 2014-12-18 上午10:18:45
 * Copyright 2014
 * Webcare 网站群监控项目组
 * All right reserved.
*/
package test;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @project TestDemo
 * @Name: com.luoan.nessus.xml.XpthByWebsoc.java
 * @Description: XXXXXXX 描述
 * @Version: V 2.0.0 （版本号）
 * @Author: liguohao
 * @date： 日期：2014-12-18 时间：上午10:18:45
 */
public class XpthByWebsoc {
	private Set<String>	set = new HashSet<String>();
	private List<String> list = new ArrayList<String>();
	static XPath xpath;
	
	
	public void TestImportOne() throws Exception{
		XPathFactory factory = XPathFactory.newInstance();
		xpath = factory.newXPath();
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = domFactory.newDocumentBuilder();
		Document document = builder.parse(new File("./src/test/websocHole1.xml"));
		NodeList bodyNode = (NodeList) xpath.evaluate("/hole//body", document,  XPathConstants.NODESET);
		System.out.println("bodyNode.getLength(): "+bodyNode.getLength());
		for (int i = 0; i < bodyNode.getLength(); i++) {
			String holeName= (String) xpath.evaluate("h3/text()", bodyNode.item(i),  XPathConstants.STRING);
			NodeList div = (NodeList) xpath.evaluate("child::div", bodyNode.item(i),  XPathConstants.NODESET);
//			System.out.println("编号："+(i+1));
			System.out.println(holeName+" "+div.getLength()+" " + "编号："+(i+1));
			Node bascInfoDiv = div.item(0);
			NodeList bascInfoNode = (NodeList) xpath.evaluate("p", bascInfoDiv,  XPathConstants.NODESET);
			getBascInfo(bascInfoNode);
			Node vulDesDiv = div.item(1);
			NodeList vulDesNode = (NodeList) xpath.evaluate("p", vulDesDiv,  XPathConstants.NODESET);
			getVulDesInfo(vulDesNode);
			System.out.println("==============================================");
		}
	}
	
	private void getBascInfo(NodeList nodeList) throws XPathExpressionException{
		for (int n = 0; n < nodeList.getLength(); n++) {
			Node node = nodeList.item(n);
			Node firstChild = node.getFirstChild();
			if("发现时间：".equals(firstChild.getTextContent())){
				System.out.println("发现时间："+firstChild.getNextSibling().getTextContent());continue;
			}
			if("更新时间：".equals(firstChild.getTextContent())){
				System.out.println("更新时间："+firstChild.getNextSibling().getTextContent());continue;
			}
			if("危险等级：".equals(firstChild.getTextContent())){
				System.out.println("危险等级："+firstChild.getNextSibling().getTextContent());continue;
			}
			if("CNNVD-ID：".equals(firstChild.getTextContent())){
				NodeList aNodeList = (NodeList) xpath.evaluate("a", node,  XPathConstants.NODESET);
				String cnnvd = "";
				for (int i = 0; i < aNodeList.getLength(); i++) {
					Node aEl = aNodeList.item(i);
					cnnvd += aEl.getTextContent()+",";
				}
				System.out.println("CNNVD-ID："+cnnvd);
				continue;
			}
			if("CVE-ID：".equals(firstChild.getTextContent())){
				NodeList aNodeList = (NodeList) xpath.evaluate("a", node,  XPathConstants.NODESET);
				String cveId = "";
				for (int i = 0; i < aNodeList.getLength(); i++) {
					Node aEl = aNodeList.item(i);
					cveId += aEl.getTextContent()+",";
				}
				System.out.println("CVE-ID："+cveId);
				continue;
			}
			if("Bugtraq ID：".equals(firstChild.getTextContent())){
				NodeList aNodeList = (NodeList) xpath.evaluate("a", node,  XPathConstants.NODESET);
				String bId = "";
				for (int i = 0; i < aNodeList.getLength(); i++) {
					Node aEl = aNodeList.item(i);
					 bId += aEl.getTextContent()+",";
				}
				System.out.println("Bugtraq ID："+bId);
				continue;
			}
			if("OWASP TOP10 漏洞描述：".equals(firstChild.getTextContent())){
				System.out.println("OWASP TOP10 漏洞描述："+node.getTextContent());
			}
		}
	}
	
	private void getVulDesInfo(NodeList nodeList){
		for (int n = 0; n < nodeList.getLength(); n++) {
			Node pNode = nodeList.item(n);
			if(n==1){
				System.err.println("漏洞影响："+pNode.getTextContent());continue;
			}
			if(n==3){
				System.err.println("漏洞概述："+pNode.getTextContent());continue;
			}
			if(n==5){
				System.err.println("建议解决方案："+pNode.getTextContent());continue;
			}
			if(n==7){
				System.err.println("参考链接："+pNode.getTextContent());
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		new XpthByWebsoc().TestImportOne();
	}
}
